// import 'whatwg-fetch'
// import 'es6-promise'
import axios from './axiosWrap.js'
import common from './common.js'

var fetchAjax = {};
fetchAjax.getData = function (url, params, callback) {

  var baseData = {
    g: 'Api1_0_0',
    user_id: common.getParam('user_id'),
    uuid: common.getParam('uuid'),
    token: common.getParam('token')
  }
  if (params) {
    params = Object.assign(params, baseData)
    let paramsArray = [];
    //拼接参数
    Object.keys(params).forEach(key => paramsArray.push(key + '=' + params[key]))
    if (url.search(/\?/) === -1) {
      url += '?' + paramsArray.join('&')
    } else {
      url += '&' + paramsArray.join('&')
    }
  }
  // fetch(url,{
  //     method: 'GET',
  //     credentials: 'include',
  //     mode: 'cors',
  //     headers:new Headers({
  //         'Content-Type': 'application/json'
  //     }),
  // }).then(res => res.json()).then(res=>{
  //     callback(res)
  // });

  axios({
    method: 'get',
    url: url,
    ifModified: true,
    headers: new Headers({
      'Content-Type': 'application/json',
      'Cache-Control': 'max-age=1',
      'If-Modified-Since':0
    })
  }).then(res => {
    callback(res.data)
  })


}

fetchAjax.getDataNew = function (url, params, callback) {

  var baseData = {
    // g: 'Api1_1_0',
    g: 'Api1_1_1',
    user_id: common.getParam('user_id'),
    uuid: common.getParam('uuid'),
    token: common.getParam('token')
  }
  if (params) {
    params = Object.assign(params, baseData)
    let paramsArray = [];
    //拼接参数
    Object.keys(params).forEach(key => paramsArray.push(key + '=' + params[key]))
    if (url.search(/\?/) === -1) {
      url += '?' + paramsArray.join('&')
    } else {
      url += '&' + paramsArray.join('&')
    }
  }
  // fetch(url,{
  //     method: 'GET',
  //     credentials: 'include',
  //     mode: 'cors',
  //     headers:new Headers({
  //         'Content-Type': 'application/json'
  //     }),
  // }).then(res => res.json()).then(res=>{
  //     callback(res)
  // });

  axios({
    method: 'get',
    url: url,
    ifModified: true,
    headers: new Headers({
      'Content-Type': 'application/json',
      'Cache-Control': 'max-age=1',
      'If-Modified-Since':0
    })
  }).then(res => {
    callback(res.data)
  })


}

fetchAjax.getDataHour = function (url, params, callback) {

  var baseData = {
    g: 'Api1_1_0',
    user_id: common.getParam('user_id'),
    uuid: common.getParam('uuid'),
    token: common.getParam('token')
  }
  if (params) {
    params = Object.assign(params, baseData)
    let paramsArray = [];
    //拼接参数
    Object.keys(params).forEach(key => paramsArray.push(key + '=' + params[key]))
    if (url.search(/\?/) === -1) {
      url += '?' + paramsArray.join('&')
    } else {
      url += '&' + paramsArray.join('&')
    }
  }
  // fetch(url,{
  //     method: 'GET',
  //     credentials: 'include',
  //     mode: 'cors',
  //     headers:new Headers({
  //         'Content-Type': 'application/json'
  //     }),
  // }).then(res => res.json()).then(res=>{
  //     callback(res)
  // });

  axios({
    method: 'get',
    url: url,
    ifModified: true,
    headers: new Headers({
      'Content-Type': 'application/json',
      'Cache-Control': 'max-age=1',
      'If-Modified-Since':0
    })
  }).then(res => {
    callback(res.data)
  })


}
// fetchAjax.postData=function(url,data,callback){

//     fetch(url,{
//         method: 'POST',
//         credentials: 'include',
//         headers:new Headers({
//             'Content-Type': 'application/json'
//         }),
//         body:JSON.stringify(data) 
//     }).then(res => res.json()).then(res=>{
//         callback(res.data)
//     });

// }

export default fetchAjax
